package com.xls.boxlibrary.risk;


import android.text.TextUtils;
import android.util.Log;

import com.hjq.gson.factory.GsonFactory;
import com.xls.boxlibrary.utils.SPUtils;
import com.xls.commonlibrary.BuildConfig;
import com.xls.commonlibrary.app.MainApplication;
import com.xls.commonlibrary.util.TokenUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class GetUrlUtil {

    private static volatile GetUrlUtil singleton = null;
    private Retrofit retrofit;


    private GetUrlUtil() {
        init();
    }

    public static GetUrlUtil getInstance() {
        if (singleton == null) {
            synchronized (GetUrlUtil.class) {
                if (singleton == null) {
                    singleton = new GetUrlUtil();
                }
            }
        }

        return singleton;
    }


    private void init() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(getHttpLoggingInterceptor());
        builder.addNetworkInterceptor(getHeaderInterceptor());

//        Logger.d("=======GetUrlUtil============baseUrl:   " + hostUrl);
        retrofit = new Retrofit.Builder()
                .client(builder.build())
                // todo 动态配置root地址
                .baseUrl(BuildConfig.BASE_COM_URL_BOX)
//                .baseUrl(hostUrl)
//                .baseUrl("https://api.weixin.qq.com/")
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(GsonFactory.getSingletonGson()))
                .build();
    }

    /**
     * 创建API
     */
    public <T> T create(Class<T> clazz) {
        return retrofit.create(clazz);
    }

    private HttpLoggingInterceptor getHttpLoggingInterceptor() {
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                // debug 才输出
                if (!BuildConfig.LOGGABLE) return;
                Log.d("HttpLoggingInterceptor", "Retrofit====Message:" + message);
            }
        });
        loggingInterceptor.setLevel(level);
        return loggingInterceptor;
    }


    private Interceptor getHeaderInterceptor() {
        // header add token
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {


                String token = TokenUtil.getInstance().getToken();

                if (TextUtils.isEmpty(token)) {
                    Request original = chain.request();
                    return chain.proceed(original);
                } else {


                    Request original = chain.request();
                    Request updateRequest = original.newBuilder()
                            .header("token", SPUtils.getLoginToken(MainApplication.getAppContext()))
                            .header("User-Type", "app")
                            .build();
                    return chain.proceed(updateRequest);
                }
            }
        };
    }

}
